package com.jy.fxf_phone.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jy.fxf_phone.R;
import com.jy.fxf_phone.utils.MyTimer;
import com.jy.fxf_phone.utils.OnTimerListener;
import com.jy.fxf_phone.widget.FoodLayout;

public class FeedActivity extends AppCompatActivity {
    private static final String TAG = FeedActivity.class.getSimpleName();
    //    Button btnStart, btnRestart, btnStop, btnReset;
//    Button btn_Food_01, btn_Food_02, btn_Food_03;
    TextView tvFoodInfo_01, tvFoodInfo_02, tvFoodInfo_03;
    FoodLayout mFoodLayout;
    TextView tvIntimacy;
    ProgressBar pbIntimacy;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    int mIntimacy;//亲密度
    int mFood_01;//食物1
    int mFood_02;//食物2
    int mFood_03;//食物3
    int mFood_04;//食物4
    int mFood_05;//食物5
    int mFood_06;//食物6


    public static final int HANDLER_UPDATE_REDUCE_BY_TIME = 0;
    public static final int REDUCE_INTIMACY = 1;//规定时间内减少的亲密度值
    public static final int REDUCE_TIME = 180;//减少亲密度值的时间（秒）
    public static final String SHARED_PREFERENCES_INTIMACY = "mIntimacy";
    public static final String SHARED_PREFERENCES_FOOD_01 = "mFood_01";
    public static final String SHARED_PREFERENCES_FOOD_02 = "mFood_02";
    public static final String SHARED_PREFERENCES_FOOD_03 = "mFood_03";
//    public static final String SHARED_PREFERENCES_FOOD_04 = "mFood_04";
//    public static final String SHARED_PREFERENCES_FOOD_05 = "mFood_05";
//    public static final String SHARED_PREFERENCES_FOOD_06 = "mFood_06";
    public static final String SHARED_PREFERENCES_DATE = "date";
    public static final String FOOD_NAME_01 = "肉丝面";
    public static final String FOOD_NAME_02 = "拉面";
    public static final String FOOD_NAME_03 = "荷包蛋";
    public static final String FOOD_NAME_04 = "蒸菜";
    public static final String FOOD_NAME_05 = "汤";
    public static final String FOOD_NAME_06 = "汉堡";

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (message.what == HANDLER_UPDATE_REDUCE_BY_TIME) {
                if (mIntimacy > REDUCE_INTIMACY) {
                    mIntimacy = mIntimacy - REDUCE_INTIMACY;
                    setIntimacy(mIntimacy);
                } else if (mIntimacy <= REDUCE_INTIMACY) {
                    mIntimacy = 0;
                    setIntimacy(mIntimacy);
                }
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        toolbar.setNavigationIcon(R.drawable.feed_back);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        sharedPreferences = getSharedPreferences("Feed_System", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        initViews();
        setDefaultIntimacy();

        MyTimer.getInstance().setOnTimerListener(new OnTimerListener() {
            @Override
            public void onTimerListener(long time) {
                Log.d("123", "已过了" + time + "秒" + "在" + Thread.currentThread().getName() + "线程");
                if (time > REDUCE_TIME) {//当时间超过x秒时发出减少亲密度指令
                    MyTimer.getInstance().start(true);
                    mHandler.sendEmptyMessage(HANDLER_UPDATE_REDUCE_BY_TIME);
                }
            }
        });
        MyTimer.getInstance().start(false);
    }


    /**
     * 重置食物数量(测试时使用)
     *
     * @param food01
     * @param food02
     * @param food03
     * @param food04
     * @param food05
     * @param food06
     */
    private void setFoodCount(int food01, int food02, int food03, int food04, int food05, int food06) {
        if (food01 > 0) {
            mFood_01 = food01;
            editor.putInt(SHARED_PREFERENCES_FOOD_01, mFood_01);
        }

        if (food02 > 0) {
            mFood_02 = food02;
            editor.putInt(SHARED_PREFERENCES_FOOD_02, mFood_02);
        }

        if (food03 > 0) {
            mFood_03 = food03;
            editor.putInt(SHARED_PREFERENCES_FOOD_03, mFood_03);
        }
//
//        if (food04 > 0) {
//            mFood_04 = food04;
//            editor.putInt(SHARED_PREFERENCES_FOOD_04, mFood_04);
//        }
//
//        if (food05 > 0) {
//            mFood_05 = food05;
//            editor.putInt(SHARED_PREFERENCES_FOOD_05, mFood_05);
//        }
//
//        if (food06 > 0) {
//            mFood_06 = food06;
//            editor.putInt(SHARED_PREFERENCES_FOOD_06, mFood_06);
//        }
        editor.apply();
        setDefaultIntimacy();
    }


    /**
     * 读取保存的亲密度值、食物数量
     */
    private void setDefaultIntimacy() {
        mIntimacy = sharedPreferences.getInt(SHARED_PREFERENCES_INTIMACY, 0);

        mFood_01 = sharedPreferences.getInt(SHARED_PREFERENCES_FOOD_01, 0);
        mFood_02 = sharedPreferences.getInt(SHARED_PREFERENCES_FOOD_02, 0);
        mFood_03 = sharedPreferences.getInt(SHARED_PREFERENCES_FOOD_03, 0);
//        mFood_04 = sharedPreferences.getInt(SHARED_PREFERENCES_FOOD_04, 0);
//        mFood_05 = sharedPreferences.getInt(SHARED_PREFERENCES_FOOD_05, 0);
//        mFood_06 = sharedPreferences.getInt(SHARED_PREFERENCES_FOOD_06, 0);
        Log.d("123", FOOD_NAME_01 + mFood_01 + FOOD_NAME_02 + mFood_02 + FOOD_NAME_03 +
                mFood_04 + FOOD_NAME_04 + mFood_04 + FOOD_NAME_05 + mFood_05 + FOOD_NAME_06 + mFood_06);

        tvFoodInfo_01.setText(FOOD_NAME_01 + ":" + mFood_01);
        tvFoodInfo_02.setText(FOOD_NAME_02 + ":" + mFood_02);
        tvFoodInfo_03.setText(FOOD_NAME_03 + ":" + mFood_03);
//        tvFoodInfo_04.setText(FOOD_NAME_04 + ":" + mFood_04);
//        tvFoodInfo_05.setText(FOOD_NAME_05 + ":" + mFood_05);
//        tvFoodInfo_06.setText(FOOD_NAME_06 + ":" + mFood_06);

        setIntimacy(mIntimacy);
    }

    /**
     * 显示亲密度并保存
     *
     * @param newIntimacy 亲密度值
     */
    private void setIntimacy(int newIntimacy) {
        if (newIntimacy <= 100) {
            pbIntimacy.setProgress(newIntimacy);
            tvIntimacy.setText("亲密度:" + newIntimacy);
            editor.putInt(SHARED_PREFERENCES_INTIMACY, newIntimacy);
            editor.apply();
        }
    }


    private void initViews() {
//        btnStart = (Button) findViewById(R.id.btnStart);
//        btnRestart = (Button) findViewById(R.id.btnRestart);
//        btnStop = (Button) findViewById(R.id.btnStop);
        pbIntimacy = (ProgressBar) findViewById(R.id.pbIntimacy);
        tvIntimacy = (TextView) findViewById(R.id.tvIntimacy);
        mFoodLayout = (FoodLayout) findViewById(R.id.foodLayout);
        tvFoodInfo_01 = (TextView) findViewById(R.id.tvFood_01);
        tvFoodInfo_02 = (TextView) findViewById(R.id.tvFood_02);
        tvFoodInfo_03 = (TextView) findViewById(R.id.tvFood_03);
//        tvFoodInfo_04 = (TextView) findViewById(R.id.tvFood_04);
//        tvFoodInfo_05 = (TextView) findViewById(R.id.tvFood_05);
//        tvFoodInfo_06 = (TextView) findViewById(R.id.tvFood_06);
        mFoodLayout.setOnFoodsClickListener(new FoodLayout.FoodsClickListener() {
            @Override
            public void onFoodsClick(int foodNumber) {
                switch (foodNumber) {
                    case 1:
                        Log.d(TAG, "onFoodsClick: " + 1);
                        if (mFood_01 > 0 && mIntimacy <= 90) {
                            mFood_01 = mFood_01 - 1;
                            tvFoodInfo_01.setText(FOOD_NAME_01 + ":" + mFood_01);
                            editor.putInt(SHARED_PREFERENCES_FOOD_01, mFood_01);
                            mIntimacy = mIntimacy + 10;
                        }
                        break;
                    case 2:
                        Log.d(TAG, "onFoodsClick: " + 2);
                        if (mFood_02 > 0 && mIntimacy <= 95) {
                            mFood_02 = mFood_02 - 1;
                            tvFoodInfo_02.setText(FOOD_NAME_02 + ":" + mFood_02);
                            editor.putInt(SHARED_PREFERENCES_FOOD_02, mFood_02);
                            mIntimacy = mIntimacy + 5;
                        }
                        break;
                    case 3:
                        Log.d(TAG, "onFoodsClick: " + 3);
                        if (mFood_03 > 0 && mIntimacy <= 99) {
                            mFood_03 = mFood_03 - 1;
                            tvFoodInfo_03.setText(FOOD_NAME_03 + ":" + mFood_03);
                            editor.putInt(SHARED_PREFERENCES_FOOD_03, mFood_03);
                            mIntimacy = mIntimacy + 1;
                        }
                        break;
//                    case 4:
//                        Log.d(TAG, "onFoodsClick: " + 4);
//                        BleUtils.getInstance().sendData("aaa", 0);
//                        if (mFood_04 > 0 && mIntimacy <= 99) {
//                            mFood_04 = mFood_04 - 1;
//                            tvFoodInfo_04.setText(FOOD_NAME_04 + ":" + mFood_04);
//                            editor.putInt(SHARED_PREFERENCES_FOOD_04, mFood_04);
//                            mIntimacy = mIntimacy + 2;
//                        }
//                        break;
//                    case 5:
//                        Log.d(TAG, "onFoodsClick: " + 5);
//                        if (mFood_05 > 0 && mIntimacy <= 99) {
//                            mFood_05 = mFood_05 - 1;
//                            tvFoodInfo_05.setText(FOOD_NAME_05 + ":" + mFood_05);
//                            editor.putInt(SHARED_PREFERENCES_FOOD_05, mFood_05);
//                            mIntimacy = mIntimacy + 3;
//                        }
//                        break;
//                    case 6:
//                        Log.d(TAG, "onFoodsClick: " + 6);
//                        if (mFood_06 > 0 && mIntimacy <= 99) {
//                            mFood_06 = mFood_06 - 1;
//                            tvFoodInfo_06.setText(FOOD_NAME_06 + ":" + mFood_06);
//                            editor.putInt(SHARED_PREFERENCES_FOOD_06, mFood_06);
//                            mIntimacy = mIntimacy + 6;
//                        }
//                        break;
                }
                setIntimacy(mIntimacy);
                editor.apply();
            }
        });

//        btnReset = (Button) findViewById(R.id.btnReset);
    }

}
